<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="渠道id">
          <el-input v-model="form.channel_id" placeholder="渠道id" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="country_code"
            label="国家编码"
            align="center"
            width="100"
          />
          <el-table-column
            prop="channel_id"
            label="支付通道id"
            align="center"
            width="120"
          />
          <el-table-column prop="currency" label="币种" align="center" />
          <el-table-column prop="fee_rate" label="通道费率" align="center" />
          <el-table-column
            prop="fixed_fee"
            label="本币单笔固定费用"
            align="center"
          />
          <el-table-column
            prop="fixed_usd_fee"
            label="美元单笔固定费用"
            align="center"
          />
          <el-table-column prop="diff_rate" label="汇差费率" align="center" />
          <el-table-column
            prop="withdraw_delay_days"
            label="提现到账天数"
            align="center"
          />
          <el-table-column
            prop="min_amount"
            label="最小支付金额"
            align="center"
          />
          <el-table-column
            prop="max_amount"
            label="最大支付金额"
            align="center"
          />
          <el-table-column
            prop="createtime"
            label="添加时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            label="操作"
            align="center"
            width="120"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >编辑</el-button
              >
              <!--<el-button type="danger"-->
              <!--    @click="handleOperate(scope.$index, scope.row, 'delete')">删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :dialogTitle="dialogTitle"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime } from "@/utils/utils";
import {
  channelCountryListApi,
  channelCountryAddApi,
  channelCountryUpdateApi,
  channelCountrylDeleteApi,
} from "@/api/ExAssistant/settings";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const btnLoading = ref(false);
const dialogTitle = ref("");
const formData = {
  channel_id: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  country_code: "",
  channel_id: "",
  currency: "",
  fee_rate: "",
  fixed_fee: "",
  fixed_usd_fee: "",
  diff_rate: "",
  min_amount: "",
  max_amount: "",
  withdraw_delay_days: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    channel_id: form.channel_id,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await channelCountryListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
//新增
const onAdd = () => {
  dialogTitle.value = "通道支持的国家列表";
  dialogVisible.value = true;
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    dialogTitle.value = "渠道国家更新";
    Object.assign(dialogForm, {
      // ...row
      channel_id: row.channel_id,
      id: row.id,
      country_code: row.country_code,
      currency: row.currency,
      fee_rate: row.fee_rate,
      fixed_usd_fee: row.fixed_usd_fee,
      fixed_fee: row.fixed_fee,
      diff_rate: row.diff_rate,
      min_amount: row.min_amount,
      max_amount: row.max_amount,
      withdraw_delay_days: row.withdraw_delay_days,
    });
    dialogVisible.value = true;
  } else if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await channelCountrylDeleteApi({ id: row.id });
      ElMessage({
        message: res.message,
        type: "success",
      });
      getTbaleData();
    });
  }
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;

  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    let res;
    if (!param.id) {
      res = await channelCountryAddApi(param);
    } else {
      res = await channelCountryUpdateApi(param);
    }

    ElMessage({
      message: res.message,
      type: "success",
    });

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
